package com.niiwoo.civet.mobile.service.bill.detail;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.dto.response.NiiwooBillRespDTO;
import com.niiwoo.civet.account.dto.response.UserBillBaseResponseDTO;
import com.niiwoo.civet.mobile.service.bill.UserBillInvestOrderService;
import com.niiwoo.civet.mobile.vo.response.bill.JumpTypeEnum;
import com.niiwoo.civet.mobile.vo.response.bill.UserBillDetailResponse;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.ProjectPackageInvestOrderDTO;
import com.niiwoo.civet.trade.service.intelligenceInvest.IntelligenceOrderDubboService;
import org.assertj.core.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 奖励账单说情
 * Created by Yijian on 2018/11/9.
 */
@Service
public class PrizeBillDetailStrategyImpl implements BillDetailStrategy {

    @Reference(version = "1.0.0")
    private IntelligenceOrderDubboService intelligenceOrderDubboService;
    @Autowired
    private UserBillInvestOrderService projectDetailService;

    @Override
    public String build(UserBillBaseResponseDTO userBillBase, List<NiiwooBillRespDTO> userBillFlowList, UserBillDetailResponse response) {
        //平台奖励对资金明细做特别处理
        List<UserBillDetailResponse.AmountItem> amountItemList = response.getAmountItems();
        if(amountItemList.size() == 1){
            //只有一条明细时不显示
            response.setAmountItems(new ArrayList<>());
        }else {
            response.setAmountItems(amountItemList);
        }


        NiiwooBillRespDTO niiwooBill = userBillFlowList.get(0);
        if(Objects.nonNull(niiwooBill)){
            UserBillDetailResponse.SpecialItem specialItem;

            //发放来源
            if(!Strings.isNullOrEmpty(niiwooBill.getRemark2())) {
                specialItem = response.new SpecialItem();
                specialItem.setName("发放来源");
                specialItem.setContent(niiwooBill.getRemark2());
                response.getSpecialItems().add(specialItem);
            }

            if(Objects.nonNull(userBillBase.getProjectId()) && Objects.nonNull(userBillBase.getInvestId())){

                boolean isNeedJump = false;
                if(userBillBase.getInvestId() > 0){
                    isNeedJump = true;
                }

                //组装投资订单
                String investOrderContent = projectDetailService.getInvestOrderContent(userBillBase, response);
                if(!userBillBase.getProjectId().equals(userBillBase.getProjectPackageId())){
                    ProjectPackageInvestOrderDTO projectPackageInvestOrderDTO = intelligenceOrderDubboService.getProjectPackageInvestOrderBySubInvestOrderId(userBillBase.getInvestId());

                    specialItem = response.new SpecialItem();
                    specialItem.setName("投资订单");
                    specialItem.setContent(investOrderContent);
                    specialItem.setIsNeedJump(isNeedJump);
                    specialItem.setJumpType(JumpTypeEnum.ZNT_INVEST_ORDER.getCode());
                    specialItem.setJumpParamOne(projectPackageInvestOrderDTO.getId().toString());
                    response.getSpecialItems().add(specialItem);
                }else {
                    specialItem = response.new SpecialItem();
                    specialItem.setName("投资订单");
                    specialItem.setContent(investOrderContent);
                    specialItem.setIsNeedJump(isNeedJump);
                    specialItem.setJumpType(JumpTypeEnum.INVEST_ORDER.getCode());
                    specialItem.setJumpParamOne(userBillBase.getInvestId().toString());
                    response.getSpecialItems().add(specialItem);
                }

                specialItem = response.new SpecialItem();
                specialItem.setName("订单编号");
                specialItem.setContent(userBillBase.getInvestId().toString());
                response.getSpecialItems().add(specialItem);
            }

        }

        return JSON.toJSONString(response);
    }
}
